package com.metalrain.alm;

import android.app.Application;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.widget.Toast;
import com.metalrain.alm.service.LCA_Service;

/**
 * Created by administrator on 2014-06-22.
 */
public class AndroidLightManagerApplication extends Application {
    private static AndroidLightManagerApplication instance;
    private LCA_Service lcaService;
    public static AndroidLightManagerApplication get() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        bindCamHueService();

    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        unbindService(bindCamHueService_ServiceConnection);
    }

    private void bindCamHueService() {
        Intent intent = new Intent(this, LCA_Service.class);
        bindService(intent, bindCamHueService_ServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Toast.makeText(AndroidLightManagerApplication.this, "Service Connected", Toast.LENGTH_SHORT).show();
                lcaService = ((LCA_Service.LocalBinder)iBinder).getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                lcaService.setSurface(null);
                lcaService = null;
            }
        }, Service.START_STICKY);
    }

    public static LCA_Service getLcaService() {
        return get().lcaService;
    }
    private ServiceConnection bindCamHueService_ServiceConnection;

}
